package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper shoppingCartMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     *  添加购物车
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 1.获取到菜品id和套餐id
        ShoppingCart shoppingCart = new ShoppingCart();
        // shoppingCartDTO ---> shoppingCart
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 获取当前用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 查询购物车是否有符合当前条件的数据
        List<ShoppingCart> list = this.shoppingCartMapper.list(shoppingCart);
        // 判断集合中是否存在数据
        if(CollectionUtils.isNotEmpty(list) && list.size() == 1){
            // 更新数量+1
            ShoppingCart sc = list.get(0);
            sc.setNumber(sc.getNumber() + 1);
            this.shoppingCartMapper.update(sc);
        }else{
            // 新增购物车数据
            // 判断是菜品还是套餐
            if(shoppingCart.getDishId() != null){
                // 根据菜品id查询菜品信息
                Dish dish = this.dishMapper.getById(shoppingCart.getDishId());
                // 菜品名称
                shoppingCart.setName(dish.getName());
                // 菜品图片
                shoppingCart.setImage(dish.getImage());
                // 菜品单价
                shoppingCart.setAmount(dish.getPrice());
            }else{
                Setmeal setmeal = this.setmealMapper.getSetmealById(shoppingCart.getSetmealId());
                // 菜品名称
                shoppingCart.setName(setmeal.getName());
                // 菜品图片
                shoppingCart.setImage(setmeal.getImage());
                // 菜品单价
                shoppingCart.setAmount(setmeal.getPrice());
            }
            // 基础数据
            // 数量
            shoppingCart.setNumber(1);
            // 创建时间
            shoppingCart.setCreateTime(LocalDateTime.now());
            // 新增到数据库
            this.shoppingCartMapper.insert(shoppingCart);
        }
    }

    /**
     *  查看当前用户的购物车
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        return this.shoppingCartMapper.list(shoppingCart);
    }

    /**
     *  根据用户id删除购物车信息
     */
    @Override
    public void deleteByUserId() {
        // 执行mapper层方法
        this.shoppingCartMapper.deleteByUserId(BaseContext.getCurrentId());
    }

    /**
     *  删除购物车中一个商品
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 获取要删除的商品信息
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        // 设置要删除购物车信息的用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        // 获取当前用户下的购物车信息
        List<ShoppingCart> list = this.shoppingCartMapper.list(shoppingCart);
        if(CollectionUtils.isNotEmpty(list) && list.size() > 0){
            shoppingCart = list.get(0);
            Integer number = shoppingCart.getNumber();
            if(number == 1){
                //当前商品在购物车中的份数为1，直接删除当前记录
                this.shoppingCartMapper.deleteById(shoppingCart.getId());
            }else {
                // 当前购物车中当前商品数量大于1
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                // 调用mapper层修改数据
                this.shoppingCartMapper.update(shoppingCart);
            }
        }
    }
}
